package com.itlong.cloud.smartcard;

import com.alibaba.fastjson.JSON;
import com.itlong.cloud.POJO.VO.smartcard.SmartCardPullTableInfoVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.project.smartcard.ISmartCardSyncDataServiceClient;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <desc>
 *      与旺龙一卡通对接-清理存于pull表中的数据的定时任务
 * </desc>
 *
 * @createDate 2018-01-26
 */
@EnableScheduling // 启用定时任务
@RestController
public class SmartCardAnalysisPullDataTimedTask {

    private static Logger LOG = LoggerFactory.getLogger(SmartCardAnalysisPullDataTimedTask.class);

    @Autowired
    ISmartCardSyncDataServiceClient iSmartCardSyncDataServiceClient;

    /**
     * <desc>
     *      与旺龙一卡通对接-处理存于pull表中的数据
     * <desc/>
     *
     * @param
     * @return
     * @author LewisLouis
     * @createDate 2018-01-26
     */
    @Scheduled(cron = "* * 2 * * ?")  //每天凌晨2点执行一次
   // @Scheduled(cron = "0 */15 * * * ?")  //每15分钟执行一次
    //    @Scheduled(cron = "0 */10 * * * ?")  //每10分钟执行一次
    public void scheduler() {

        List<SmartCardPullTableInfoVO> pullTableInfoVOS =  iSmartCardSyncDataServiceClient.getAllPullTablesInfo();

        if ((null == pullTableInfoVOS) || (pullTableInfoVOS.isEmpty())){
//            LOG.info("没有发现需要处理到一卡通Pull表,放弃清理任务");
            return;
        }

        for (SmartCardPullTableInfoVO tableInfoVO:pullTableInfoVOS){
            if (StringUtils.isBlank(tableInfoVO.getTableName())|| (null == tableInfoVO.getTableRows())){
                LOG.info(String.format("一卡通Pull表:%s信息非法,跳过清理", JSON.toJSONString(tableInfoVO)));
                continue;
            }
            try {
                if (tableInfoVO.getTableRows() < PlatformConstants.SMARTCARD_KEEP_PASSRECORD_PER_PROJECT){
                    //LOG.info(String.format("一卡通Pull表:%s 的数据量较少,暂不清理", JSON.toJSONString(tableInfoVO)));
                    continue;
                }
                iSmartCardSyncDataServiceClient.movePullDataToColdTable(tableInfoVO.getTableName());
//                LOG.info(String.format("一卡通Pull表:%s 的数据量较多,成功清理", JSON.toJSONString(tableInfoVO)));

            } catch (Exception e) {
                LOG.error(String.format("清理一卡通Pull表:%s失败", JSON.toJSONString(tableInfoVO)),e);
                return;
            }
        }
//        LOG.info("成功转移所有一卡通Pull表过期数据");

    }
}
